<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.find{width: 250px; float: left;}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
	resetForm();
	
	$('#line_no').change(function () {
		$.ajax({
			type:"post",
			url:"major.do",
			data:"command=getSublineList&&line_no="+$(this).val(),
			
			success:function(data){
				var sublinehtml="<option value=\"0\">세부계열</option>";
				var list=data.sublinelist;
				for(var i=0;i<list.length;i++){
					sublinehtml+="<option value=\""+list[i].subline_no+"\">"+list[i].subline_name+"</option>";
				}
				$('#subline_no').html(sublinehtml);
			}
		});
	});
	
	$('#findMajorunit').keyup(function() {
		searchMajorunit();
	});
	
	$('#findUniv').keyup(function() {
		searchUniv();
	});
	
	$('#findCollege').keyup(function() {
		searchCollege();
	});
	
	$('#findFaculty').keyup(function() {
		searchFaculty();
	});
	
	$('#findMajor').keyup(function() {
		searchMajor();
	});
	
	
	$('#noCollege').click(function() {
		resetCollege();
	});
	
	$('#noFaculty').click(function() {
		resetFaculty();
	});
	
	$('#noMajor').click(function() {
		resetMajor();
	});
	
	$(document).on('click', '.majorunit', function() {
		var unit=$(this);
		$('#majorunit_no').val(unit.attr('name'));
		$('#univ_no').val(unit.find('.univ_name').attr('name'));
		$('#univ_name').text(unit.find('.univ_name').text());
		$('#college_no').val(unit.find('.college_name').attr('name'));
		$('#college_name').text(unit.find('.college_name').text());
		$('#faculty_no').val(unit.find('.faculty_name').attr('name'));
		$('#faculty_name').text(unit.find('.faculty_name').text());
		$('#major_no').val(unit.find('.major_name').attr('name'));
		$('#major_name').text(unit.find('.major_name').text());
		$('#line_no').find($('option[value='+unit.find('.line_no').attr('name')+']')).prop('selected', true);
		$.ajax({
			type:"post",
			url:"major.do",
			data:"command=getSublineList&&line_no="+$('#line_no').val(),
			
			success:function(data){
				var sublinehtml="<option value=\"0\">세부계열</option>";
				var list=data.sublinelist;
				for(var i=0;i<list.length;i++){
					sublinehtml+="<option value=\""+list[i].subline_no+"\">"+list[i].subline_name+"</option>";
				}
				$('#subline_no').html(sublinehtml);
				$('#subline_no').find($('option[value='+unit.find('.subline_no').attr('name')+']')).prop('selected', true);
				$('#command').val('updateMajorunit');
				$('#submitBtn').html('수정');
				$('#resetBtn').show();
				$('#delBtn').show();
				$('#findMajorunit').val('');
				$('#majorunitlist *').remove();
			}
		});
		
	});
	
	$(document).on('click', '.univ', function() {
		$('#univ_no').val($(this).attr('name'));
		$('#univ_name').text($(this).find('.univ_name').text());
		$('#findUniv').val('');
		$('.univ').remove();
	});
	
	$(document).on('click', '.college', function() {
		$('#college_no').val($(this).attr('name'));
		$('#college_name').text($(this).find('.college_name').text());
		$('#findCollege').val('');
		$('.college').remove();
	});
	
	$(document).on('click', '.faculty', function() {
		$('#faculty_no').val($(this).attr('name'));
		$('#faculty_name').text($(this).find('.faculty_name').text());
		$('#findFaculty').val('');
		$('.faculty').remove();
	});
	
	$(document).on('click', '.major', function() {
		$('#major_no').val($(this).attr('name'));
		$('#major_name').text($(this).find('.major_name').text());
		$('#findMajor').val('');
		$('.major').remove();
	});
	
	
	$('#submitBtn').click(function() {
		$('#editForm').submit();
	});
	
	$('#delBtn').click(function() {
		location.href="major.do?command=deleteMajorunit&&majorunit_no="+$('#majorunit_no').val();
	});
	
	$('#resetBtn').click(function() {
		resetForm();
	});
	
});

function checkNull(str) {
	if(str==null) return "";
	else return str;
}

function searchMajorunit() {
	var majorunithtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getMajorunitList&&search="+$('#findMajorunit').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.majorunitlist;
			for (var i=0;i<list.length;i++) {
				majorunithtml+=
					"<div class='majorunit' name='"+list[i].majorunit_no+"'>"+
						"<span class='univ_name' name='"+list[i].univ.univ_no+"'>"+checkNull(list[i].univ.univ_name)+"</span> "+
						"<span class='college_name' name='"+list[i].college.college_no+"'>"+checkNull(list[i].college.college_name)+"</span> "+
						"<span class='faculty_name' name='"+list[i].faculty.faculty_no+"'>"+checkNull(list[i].faculty.faculty_name)+"</span> "+
						"<span class='major_name' name='"+list[i].major.major_no+"'>"+checkNull(list[i].major.major_name)+"</span>"+
						"<span class='line_no' name='"+list[i].subline.line.line_no+"'></span>"+
						"<span class='subline_no' name='"+list[i].subline.subline_no+"'></span>"+
					"</div>";
			}
			$('#majorunitlist').html(majorunithtml);
		}
	});
}

function searchUniv() {
	var univhtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getUnivList&&search="+$('#findUniv').val(),
		dataType: "json",
		
		success:function(data){
			var list=data.univlist;
			for (var i=0;i<list.length;i++) {
				univhtml+="<div class='univ' name='"+list[i].univ_no+"'><div class='univ_name'>"+list[i].univ_name+"</div></div>";
			}
			$('#univlist').html(univhtml);
		}
	});
}

function searchCollege() {
	var collegehtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getCollegeList&&search="+$('#findCollege').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.collegelist;
			for (var i=0;i<list.length;i++) {
				collegehtml+="<div class='college' name='"+list[i].college_no+"'><div class='college_name'>"+list[i].college_name+"</div></div>";
			}
			$('#collegelist').html(collegehtml);
		}
	});
}

function searchFaculty() {
	var facultyhtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getFacultyList&&search="+$('#findFaculty').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.facultylist;
			for (var i=0;i<list.length;i++) {
				facultyhtml+="<div class='faculty' name='"+list[i].faculty_no+"'><div class='faculty_name'>"+list[i].faculty_name+"</div></div>";
			}
			$('#facultylist').html(facultyhtml);
		}
	});
}

function searchMajor() {
	var majorhtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getMajorList&&search="+$('#findMajor').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.majorlist;
			for (var i=0;i<list.length;i++) {
				majorhtml+="<div class='major' name='"+list[i].major_no+"'><div class='major_name'>"+list[i].major_name+"</div></div>";
			}
			$('#majorlist').html(majorhtml);
		}
	});
}

function resetUniv() {
	$('#univ_no').val('-999');
	$('#univ_name').text('');
	$('#findUniv').val('');
	$('.univ').remove();
}

function resetCollege() {
	$('#college_no').val('-999');
	$('#college_name').text('');
	$('#findCollege').val('');
	$('.college').remove();
}

function resetFaculty() {
	$('#faculty_no').val('-999');
	$('#faculty_name').text('');
	$('#findFaculty').val('');
	$('.faculty').remove();
}

function resetMajor() {
	$('#major_no').val('-999');
	$('#major_name').text('');
	$('#findMajor').val('');
	$('.major').remove();
}

function resetForm() {
	$('#command').val('registerMajorunit');
	$('#major_no').val('-999');
	$('#findMajorunit').val('');
	resetUniv();
	resetCollege();
	resetFaculty();
	resetMajor();
	$('#submitBtn').html('등록');
	$('#resetBtn').hide();
	$('#delBtn').hide();
	$('#line_no').find($('option[value="0"]')).prop('selected', true);
	$('#line_no').trigger('change');
}

</script>
</head>
<body>
<a href="index.do?command=menu">메뉴로</a>
<div id="findDiv">
	<div id="findMajorunitDiv">
		<input id="findMajorunit">
		<div id="majorunitlist"></div>
	</div>
</div>
<div id="editDiv">
	<div id="findUnivDiv" class="find">
		대학<br><input id="findUniv">
		<div id="univlist"></div>
	</div>
	<div id="findCollegeDiv" class="find">
		단대<br><input id="findCollege"><span id="noCollege">없음</span>
		<div id="collegelist"></div>
	</div>
	<div id="findFacultyDiv" class="find">
		학부<br><input id="findFaculty"><span id="noFaculty">없음</span>
		<div id="facultylist"></div>
	</div>
	<div id="findMajorDiv" class="find">
		학과<br><input id="findMajor"><span id="noMajor">없음</span>
		<div id="majorlist"></div>
	</div>
	<div id="formDiv" style="width: 300; float: left;">
		<div id="nameResultDiv">
			<span id="univ_name"></span> <span id="college_name"></span> <span id="faculty_name"></span> <span id="major_name"></span>
		</div>
		<form action="major.do" method="post" id="editForm">
			<input type="hidden" id="command" name="command" value="registerMajorunit">
			<input type="hidden" id="majorunit_no" name="majorunit_no" value="-999">
			<input type="hidden" id="univ_no" name="univ.univ_no" value="-999">
			<input type="hidden" id="college_no" name="college.college_no" value="-999">
			<input type="hidden" id="faculty_no" name="faculty.faculty_no" value="-999">
			<input type="hidden" id="major_no" name="major.major_no" value="-999">
			<select id="line_no">
				<option value="0">계열</option>
				<c:forEach var="line" items="${requestScope.linelist}">
					<option value="${line.line_no}">${line.line_name}</option>
				</c:forEach>
			</select>
			<select name="subline.subline_no" id="subline_no">
				<option value="0">세부계열</option>
			</select>
		</form>
		<div id="submitBtn">등록</div>
		<div id="delBtn">삭제</div>
		<div id="resetBtn">새 학과유닛 등록</div>
	</div>
</div>
</body>
</html>